//
//  UINavigationController+SWNavigationController.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/14.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "UINavigationController+SWNavigationController.h"
#import "UIBarButtonItem+SWBarButtobItem.h"
#import <objc/runtime.h>

@implementation UINavigationController (SWNavigationController)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class = [self class];
        
        Method pushVC = class_getInstanceMethod(class, @selector(pushViewController:animated:));
        Method sw_pushVC = class_getInstanceMethod(class, @selector(sw_pushViewController:animated:));
        method_exchangeImplementations(pushVC, sw_pushVC);
    });
}

- (void)sw_pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        //解决按钮不靠左 靠右的问题.
//        UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
//        nagetiveSpacer.width = -20;//这个值可以根据自己需要自己调整
//        viewController.navigationItem.leftBarButtonItems = @[nagetiveSpacer,[UIBarButtonItem itemWithIcon:@"tongyong_fanhui" highIcon:@"" target:self action:@selector(popViewControllerAnimated:)]];
        
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"tongyong_fanhui" highIcon:@"" target:self action:@selector(popViewControllerAnimated:)];
        
//        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"icon_left_back" highIcon:@"" target:self action:@selector(popViewControllerAnimated:)];
    }
    
    [self sw_pushViewController:viewController animated:animated];
}

@end
